/*  
* Copyright 2013-2020 Smartdot Technologies Co., Ltd. All rights reserved.  * SMARTDOT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.  * 
*/ 
package com.ssm.zhou.project.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.zhou.project.entity.Project;
import com.ssm.zhou.project.exception.ProjectException;
import com.ssm.zhou.project.service.ProjectService;

/** 
 * <p>  
 *   
 * @author <a href="mailto:zhoujb@smartdot.com.cn">周竞博</a>  * @version 1.0, 2020年6月2日 
 */
@Controller
@RequestMapping(value="/project")
public class ProjectController {
	private static Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
	@Autowired
	private ProjectService projectService;

	@RequestMapping(value="/submit",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String projectSubmit(Project project, HttpServletRequest req){
		
		System.out.println(project);
		String msg = "保存成功";
		String url = "/projectMaintain";
		// String url = "/index.jsp";
		StringBuffer strScript = new StringBuffer();
		try{
			projectService.insertProject(project);
		    url = "/project/list.do?pageNum=1&pageSize=10";
		}catch (ProjectException ep) {
			msg = ep.getMessage();
		}
		strScript.append("<script type=\"text/javascript\">");
		strScript.append("alert(\"" + msg + "\");");
		strScript.append("parent.location.href =\"" + req.getContextPath() + url+  "\";");
		strScript.append("</script>");
		return strScript.toString();
	}
	@RequestMapping(value="/list")
	public String getProjectList(Model model, Integer pageSize, @RequestParam(value="pageNum",defaultValue="1")Integer pageNum){
		
		System.out.println("------------测试乱码");
		try{
			// 查询之前调用 传入页码和每页大小
			PageHelper.startPage(pageNum.intValue(), pageSize.intValue());
			// 紧跟PageHelper的第一个select会被分页
			List<Project> projects = projectService.selectAllProject();
			// 使用pageInfo对象封装结果
			PageInfo page = new PageInfo(projects);
			// 把pageInfo存入请求域
			System.out.println(page);
			model.addAttribute("pageInfo", page);

		}
		catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			throw new RuntimeException(e);
		}
		return "projectList";
	}
	/**
	 * 通过ID删除
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String deleteProjectById(@RequestParam("id") String id){
		String msg = "";
		try {
			if(! (projectService.deleteProject(id) > 1)){
				msg = "<mydiv>0</mydiv>";
			}
			msg = "<mydiv>1</mydiv>";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			throw new RuntimeException(e);
		}
		return msg;
	}
	/**
	 * 编辑
	 * 先通过ID查找，再把数据返回给页面
	 */
	@RequestMapping("/edit")
	public String editProject(@RequestParam(required = true,name="id") String id,Model model){
		try {		
			Project project = projectService.sesectProjecyById(id);
			model.addAttribute("project",project);
			System.out.println(project);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			throw new RuntimeException(e);
		}
		return "projectEdit";
	}
	/**
	 * 编辑保存调用
	 */
	@RequestMapping(value="/editSubmit",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String projectEditSubmit(Project project, HttpServletRequest req){
		
		System.out.println(project);
		String msg = "保存成功";
		String url = "/projectMaintain";
		// String url = "/index.jsp";
		StringBuffer strScript = new StringBuffer();
		try{
			projectService.updateProject(project);
			url = "/project/list.do?pageNum=1&pageSize=10";
		}catch (ProjectException ep) {
			msg = ep.getMessage();
		}
		strScript.append("<script type=\"text/javascript\">");
		strScript.append("alert(\"" + msg + "\");");
		strScript.append("parent.location.href =\"" + req.getContextPath() + url+  "\";");
		strScript.append("</script>");
		return strScript.toString();
	}
	@RequestMapping(value="/excelImport",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String excelImport(HttpServletRequest request,HttpServletResponse response,MultipartFile file){
		String msg = null;
		
  
	 	logger.info("=======excelImportController========");

	 	try {
	 		msg = projectService.excelImport(request,response);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			msg = e.toString();
		}
		System.out.println("msg====" + msg);
		return msg;
	}
	@RequestMapping(value="/search")
	public String selectBykeyWord(Model model,@RequestParam(value="pageNum",defaultValue="1")Integer pageNum,@RequestParam(value="pageSize",defaultValue="10")Integer pageSize, @RequestParam("search") String keyWord){
		System.out.println("------------selectBykeyWord");
		// 查询之前调用 传入页码和每页大小
		PageHelper.startPage(pageNum.intValue(), pageSize.intValue());
		// 紧跟PageHelper的第一个select会被分页
		List<Project> projects = projectService.selectBykeyWord(keyWord);
		// 使用pageInfo对象封装结果
		PageInfo page = new PageInfo(projects);
		// 把pageInfo存入请求域
		System.out.println(page);
		model.addAttribute("keyWord", keyWord);
		model.addAttribute("pageInfo", page);
		return "projectList";	
	}
}
 